USE [NANS21V]
GO

/****** 긆긳긙긃긏긣:  StoredProcedure [dbo].[TimeTableList]    긚긏깏긵긣볷븊: 01/29/2019 14:26:02 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TimeTableList]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[TimeTableList]
GO

CREATE  PROCEDURE [dbo].[TimeTableList]
	@TAIKAINO int,
	@HIDUKE varchar(8),
	@SYUMOKU varchar,
	@SEIBETUNO varchar

--ALTER PROCEDURE [dbo].[TimeTableList]
--	@TAIKAINO int,
--	@HIDUKE varchar(8),
--	@SYUMOKU varchar,
--	@SEIBETUNO varchar
AS

Declare @test varchar(100);

IF @SYUMOKU='1'
BEGIN
	SELECT (CONVERT(varchar, KG.TAIKAINO) + '-' + 
	        CONVERT(varchar, TKU.KYOGINO) + '-' + 
		    CONVERT(varchar, TRO.ROUNDNO) + '-' + 
		    CONVERT(varchar, TKU.SUBNO) + '-' + 
			CONVERT(varchar, TRO.KUMISUU) + '-' + 
			CONVERT(varchar, KG.SYUMOKUKUBUN) + '-' + 
			CONVERT(varchar, KG.SUBSYUMOKUKUBUN) + '-' + 
			CONVERT(varchar, KG.GATTAIFLG)
			
			 + '-' + CONVERT(varchar,KG.SUBKIROKUKUBUN)
		   ) AS KEKKAFLG,
		   SUBSTRING(TKU.KAISI,1,2) + ':' + SUBSTRING(TKU.KAISI,3,4) AS KAISI,
		   SUBSTRING(TKU.SYOSYU,1,2) + ':' + SUBSTRING(TKU.SYOSYU,3,4) AS SYOSYU,
		   CONVERT(varchar, dbo.Basyo(TKU.BASYO)) AS BASYO,
		   KG.KYOGIMEI ,
		   TRO.ROUNDMEI,
		   TKU.KUMINO,
		   CONVERT(varchar, dbo.Tyakudori(TRO.TYAKUDORI, TRO.PLUS)) AS TYAKUDORI,
		   dbo.RollCall(TKU.HOST, TKU.ROLLCALLFLG, TKU.KINOUFLG)AS ROLLCALLFLG,
		   TRO.TUKAKIROKU,
		   dbo.Kumibun(TRO.HOST, TRO.KUMIWAKEFLG) AS KUMIWAKEFLG,
		   dbo.Result(KG.SYUMOKUKUBUN,TKU.KYOGIFLG, TKU.KINOUFLG, TKU.HOST) AS RESULT,
		   dbo.KyogiFlg(TRO.HOST,TKU.KYOGIFLG) AS KYOGIFLG
		   -- 20190212 뢇맫SEQ.999 걁빥귂룈뢇맫걂
		   --,ROW_NUMBER() OVER (PARTITION BY KG.TAIKAINO ORDER BY TKU.KAISI, KG.SEIBETUNO, KG.SYUBETU_PRINTNO, KG.SYUMOKUNO, KG.KONSEIID) AS NUMBER
		  -- 20190612 뢇맫SEQ.999 걁빥귂룈뢇맫걂
		   --, ROW_NUMBER() OVER (PARTITION BY KG.TAIKAINO ORDER BY TKU.KAISI, KG.SEIBETUNO, KG.SYUBETU_PRINTNO, KG.SYUMOKU_PRINTNO, KG.KONSEIID) AS NUMBER
		   , ROW_NUMBER() OVER (PARTITION BY KG.TAIKAINO ORDER BY TKU.KAISI, KG.SEIBETUNO, KG.SYUBETU_PRINTNO, KG.SYUMOKU_PRINTNO, KG.KONSEIID, TKU.KUMINO) AS NUMBER
	  FROM TBL_KUMI AS TKU, 
	       TBL_ROUND AS TRO,
	       --KG TBL
		   (SELECT TK.TAIKAINO, 
				   TK.KYOGINO, 
				   TK.KYOGIMEI + SUB.SUBMEI AS KYOGIMEI, 
				   TK.KYOGIKANA, 
				   TK.SEIBETUNO, 
				   TSM.SYUMOKUNO, 
				   TSM.SYUMOKUMEI, 
				   TSM.KIROKUKUBUN, 
				   TSM.SYUMOKUKUBUN,
				   SUB.SUBMEI, 
				   SUB.SUBNO, 
				   SUB.KIROKUKUBUN AS SUBKIROKUKUBUN, 
				   SUB.SYUMOKUKUBUN AS SUBSYUMOKUKUBUN, 
				   TS.PRINTNO AS SYUBETU_PRINTNO, 
				   -- 20190212 뢇맫SEQ.999 걁빥귂룈뢇맫걂
				   --SUB.PRINTNO AS SYUMOKU_PRINTNO, 
				   TSM.PRINTNO AS SYUMOKU_PRINTNO, 
				   SUB.KONSEIID, 
				   TK.GATTAIFLG
				  
			 FROM TBL_KYOGI AS TK, 
				  TBL_SYUBETU AS TS, 
				  TBL_SYUMOKU_MASTER AS TSM, 
				  (SELECT K.TAIKAINO, 
						  K.KYOGINO, 
						  K.SYUMOKUNO, 
						  K.KYOGIMEI, 
						  K.KYOGIKANA, 
						  0 AS SUBNO, 
						  '' AS SUBMEI,
						  K.SEIBETUNO, 
						  SM.KIROKUKUBUN, 
						  SM.SYUMOKUKUBUN, 
						  SM.PRINTNO, 
						  0 AS KONSEIID
						 ,0 AS KONSEINO
					FROM TBL_KYOGI AS K, 
						 TBL_SYUMOKU_MASTER AS SM
					  WHERE K.TAIKAINO = @TAIKAINO
						AND K.SYUMOKUNO = SM.SYUMOKUNO
						AND (SM.KONSEISYUMOKUSUU = 0 OR SM.KONSEISYUMOKUSUU IS NULL)
					UNION
					  SELECT K.TAIKAINO, 
							 K.KYOGINO, 
							 K.SYUMOKUNO, 
							 K.KYOGIMEI, 
							 K.KYOGIKANA, 
							 SM.SYUMOKUNO AS SUBNO, 
							 SM.SYUMOKUMEI AS SUBMEI,
							 K.SEIBETUNO, 
							 SM.KIROKUKUBUN, 
							 SM.SYUMOKUKUBUN, 
							 SM.PRINTNO, 
							 KM.ID AS KONSEIID
							 ,KM.KONSEINO AS KONSEINO
						FROM TBL_KYOGI AS K, 
							 TBL_SYUMOKU_MASTER AS SM, 
							 TBL_KONSEI_MASTER AS KM
						WHERE K.TAIKAINO = @TAIKAINO
						  AND K.SYUMOKUNO = KM.SYUMOKUNO
						  AND KM.KONSEINO = SM.SYUMOKUNO
				  ) AS SUB
			 WHERE TK.TAIKAINO = @TAIKAINO
			   AND TK.TAIKAINO = SUB.TAIKAINO
			   AND TK.SYUMOKUNO = SUB.SYUMOKUNO
			   AND TK.SYUMOKUNO = TSM.SYUMOKUNO
			   AND TK.KYOGINO = SUB.KYOGINO
			   AND TS.SYUBETUNO = TK.SYUBETUNO
			   AND (TK.GATTAIFLG = 0 OR TK.GATTAIFLG = 2)
		   ) AS KG
		   WHERE TKU.TAIKAINO = @TAIKAINO
			 AND TKU.TAIKAINO = KG.TAIKAINO
			 AND TKU.KYOGINO = KG.KYOGINO
			 AND TKU.SUBNO = KG.SUBNO
			 AND TKU.TAIKAINO = TRO.TAIKAINO
			 AND TKU.KYOGINO = TRO.KYOGINO
			 AND TKU.SUBNO = TRO.SUBNO
			 AND TKU.ROUNDNO = TRO.ROUNDNO
			 AND TRO.HIDUKE LIKE @HIDUKE

AND KG.SYUMOKUKUBUN in (1,2,3,4,12,8)
AND (SUBSYUMOKUKUBUN LIKE CASE WHEN KG.SYUMOKUKUBUN = 8 THEN 1 ELSE KG.SYUMOKUKUBUN END
OR SUBSYUMOKUKUBUN LIKE CASE WHEN KG.SYUMOKUKUBUN = 8 THEN 2 ELSE KG.SYUMOKUKUBUN END
OR SUBSYUMOKUKUBUN LIKE CASE WHEN KG.SYUMOKUKUBUN = 8 THEN 3 ELSE KG.SYUMOKUKUBUN END
OR SUBSYUMOKUKUBUN LIKE CASE WHEN KG.SYUMOKUKUBUN = 8 THEN 4 ELSE KG.SYUMOKUKUBUN END
OR SUBSYUMOKUKUBUN LIKE CASE WHEN KG.SYUMOKUKUBUN = 8 THEN 12 ELSE KG.SYUMOKUKUBUN END)

--
			 AND KG.SEIBETUNO LIKE @SEIBETUNO
	-- 20190212 뢇맫SEQ.999 걁빥귂룈뢇맫걂
	--ORDER BY TKU.KAISI, KG.SEIBETUNO, KG.SYUBETU_PRINTNO, KG.SYUMOKUNO, KG.KONSEIID
	-- 20190612 뢇맫SEQ.999 걁빥귂룈뢇맫걂
	--ORDER BY TKU.KAISI, KG.SEIBETUNO, KG.SYUBETU_PRINTNO, KG.SYUMOKU_PRINTNO, KG.KONSEIID
	ORDER BY TKU.KAISI, KG.SEIBETUNO, KG.SYUBETU_PRINTNO, KG.SYUMOKU_PRINTNO, KG.KONSEIID, TKU.KUMINO

END
else IF @SYUMOKU='2'
BEGIN
	SELECT (CONVERT(varchar, KG.TAIKAINO) + '-' + 
	        CONVERT(varchar, TKU.KYOGINO) + '-' + 
		    CONVERT(varchar, TRO.ROUNDNO) + '-' + 
		    CONVERT(varchar, TKU.SUBNO) + '-' + 
			CONVERT(varchar, TRO.KUMISUU) + '-' + 
			CONVERT(varchar, KG.SYUMOKUKUBUN) + '-' + 
			CONVERT(varchar, KG.SUBSYUMOKUKUBUN) + '-' + 
			CONVERT(varchar, KG.GATTAIFLG)
			 + '-' + CONVERT(varchar,KG.SUBKIROKUKUBUN)
		   ) AS KEKKAFLG,
		   SUBSTRING(TKU.KAISI,1,2) + ':' + SUBSTRING(TKU.KAISI,3,4) AS KAISI,
		   SUBSTRING(TKU.SYOSYU,1,2) + ':' + SUBSTRING(TKU.SYOSYU,3,4) AS SYOSYU,
		   CONVERT(varchar, dbo.Basyo(TKU.BASYO)) AS BASYO,
		   KG.KYOGIMEI ,
		   TRO.ROUNDMEI,
		   TKU.KUMINO,
		   CONVERT(varchar, dbo.Tyakudori(TRO.TYAKUDORI, TRO.PLUS)) AS TYAKUDORI,
		   dbo.RollCall(TKU.HOST, TKU.ROLLCALLFLG, TKU.KINOUFLG)AS ROLLCALLFLG,
		   TRO.TUKAKIROKU,
		   dbo.Kumibun(TRO.HOST, TRO.KUMIWAKEFLG) AS KUMIWAKEFLG,
		   dbo.Result(KG.SYUMOKUKUBUN,TKU.KYOGIFLG, TKU.KINOUFLG, TKU.HOST) AS RESULT,
		   dbo.KyogiFlg(TRO.HOST,TKU.KYOGIFLG) AS KYOGIFLG
		   -- 20190212 뢇맫SEQ.999 걁빥귂룈뢇맫걂
		   --,ROW_NUMBER() OVER (PARTITION BY KG.TAIKAINO ORDER BY TKU.KAISI, KG.SEIBETUNO, KG.SYUBETU_PRINTNO, KG.SYUMOKUNO, KG.KONSEIID) AS NUMBER
	       -- 20190612 뢇맫SEQ.999 걁빥귂룈뢇맫걂
		   --, ROW_NUMBER() OVER (PARTITION BY KG.TAIKAINO ORDER BY TKU.KAISI, KG.SEIBETUNO, KG.SYUBETU_PRINTNO, KG.SYUMOKU_PRINTNO, KG.KONSEIID) AS NUMBER
		   , ROW_NUMBER() OVER (PARTITION BY KG.TAIKAINO ORDER BY TKU.KAISI, KG.SEIBETUNO, KG.SYUBETU_PRINTNO, KG.SYUMOKU_PRINTNO, KG.KONSEIID, TKU.KUMINO) AS NUMBER
	  FROM TBL_KUMI AS TKU, 
	       TBL_ROUND AS TRO,
	       --KG TBL
		   (SELECT TK.TAIKAINO, 
				   TK.KYOGINO, 
				   TK.KYOGIMEI + SUB.SUBMEI AS KYOGIMEI, 
				   TK.KYOGIKANA, 
				   TK.SEIBETUNO, 
				   TSM.SYUMOKUNO, 
				   TSM.SYUMOKUMEI, 
				   TSM.KIROKUKUBUN, 
				   TSM.SYUMOKUKUBUN,
				   SUB.SUBMEI, 
				   SUB.SUBNO, 
				   SUB.KIROKUKUBUN AS SUBKIROKUKUBUN, 
				   SUB.SYUMOKUKUBUN AS SUBSYUMOKUKUBUN, 
				   TS.PRINTNO AS SYUBETU_PRINTNO, 
				   -- 20190212 뢇맫SEQ.999 걁빥귂룈뢇맫걂
--				   SUB.PRINTNO AS SYUMOKU_PRINTNO, 
				   TSM.PRINTNO AS SYUMOKU_PRINTNO,
				   SUB.KONSEIID, 
				   TK.GATTAIFLG

			 FROM TBL_KYOGI AS TK, 
				  TBL_SYUBETU AS TS, 
				  TBL_SYUMOKU_MASTER AS TSM, 
				  (SELECT K.TAIKAINO, 
						  K.KYOGINO, 
						  K.SYUMOKUNO, 
						  K.KYOGIMEI, 
						  K.KYOGIKANA, 
						  0 AS SUBNO, 
						  '' AS SUBMEI,
						  K.SEIBETUNO, 
						  SM.KIROKUKUBUN, 
						  SM.SYUMOKUKUBUN, 
						  SM.PRINTNO, 
						  0 AS KONSEIID
						 ,0 AS KONSEINO
					FROM TBL_KYOGI AS K, 
						 TBL_SYUMOKU_MASTER AS SM
					  WHERE K.TAIKAINO = @TAIKAINO
						AND K.SYUMOKUNO = SM.SYUMOKUNO
						AND (SM.KONSEISYUMOKUSUU = 0 OR SM.KONSEISYUMOKUSUU IS NULL)
					UNION
					  SELECT K.TAIKAINO, 
							 K.KYOGINO, 
							 K.SYUMOKUNO, 
							 K.KYOGIMEI, 
							 K.KYOGIKANA, 
							 SM.SYUMOKUNO AS SUBNO, 
							 SM.SYUMOKUMEI AS SUBMEI,
							 K.SEIBETUNO, 
							 SM.KIROKUKUBUN, 
							 SM.SYUMOKUKUBUN, 
							 SM.PRINTNO, 
							 KM.ID AS KONSEIID
							 ,KM.KONSEINO AS KONSEINO
						FROM TBL_KYOGI AS K, 
							 TBL_SYUMOKU_MASTER AS SM, 
							 TBL_KONSEI_MASTER AS KM
						WHERE K.TAIKAINO = @TAIKAINO
						  AND K.SYUMOKUNO = KM.SYUMOKUNO
						  AND KM.KONSEINO = SM.SYUMOKUNO
				  ) AS SUB
			 WHERE TK.TAIKAINO = @TAIKAINO
			   AND TK.TAIKAINO = SUB.TAIKAINO
			   AND TK.SYUMOKUNO = SUB.SYUMOKUNO
			   AND TK.SYUMOKUNO = TSM.SYUMOKUNO
			   AND TK.KYOGINO = SUB.KYOGINO
			   AND TS.SYUBETUNO = TK.SYUBETUNO
			   AND (TK.GATTAIFLG = 0 OR TK.GATTAIFLG = 2)
		   ) AS KG
		   WHERE TKU.TAIKAINO = @TAIKAINO
			 AND TKU.TAIKAINO = KG.TAIKAINO
			 AND TKU.KYOGINO = KG.KYOGINO
			 AND TKU.SUBNO = KG.SUBNO
			 AND TKU.TAIKAINO = TRO.TAIKAINO
			 AND TKU.KYOGINO = TRO.KYOGINO
			 AND TKU.SUBNO = TRO.SUBNO
			 AND TKU.ROUNDNO = TRO.ROUNDNO
			 AND TRO.HIDUKE LIKE @HIDUKE

AND KG.SYUMOKUKUBUN in (5,6,8)
AND (SUBSYUMOKUKUBUN LIKE CASE WHEN KG.SYUMOKUKUBUN = 8 THEN 6 ELSE KG.SYUMOKUKUBUN END
OR SUBSYUMOKUKUBUN LIKE CASE WHEN KG.SYUMOKUKUBUN = 8 THEN 5 ELSE KG.SYUMOKUKUBUN END)
--
			 AND KG.SEIBETUNO LIKE @SEIBETUNO
	-- 20190212 뢇맫SEQ.999 걁빥귂룈뢇맫걂
	--ORDER BY TKU.KAISI, KG.SEIBETUNO, KG.SYUBETU_PRINTNO, KG.SYUMOKUNO, KG.KONSEIID
	-- 20190612 뢇맫SEQ.999 걁빥귂룈뢇맫걂
	--ORDER BY TKU.KAISI, KG.SEIBETUNO, KG.SYUBETU_PRINTNO, KG.SYUMOKU_PRINTNO, KG.KONSEIID
	ORDER BY TKU.KAISI, KG.SEIBETUNO, KG.SYUBETU_PRINTNO, KG.SYUMOKU_PRINTNO, KG.KONSEIID, TKU.KUMINO
END
else IF @SYUMOKU='3'
BEGIn
	SELECT (CONVERT(varchar, KG.TAIKAINO) + '-' + 
	        CONVERT(varchar, TKU.KYOGINO) + '-' + 
		    CONVERT(varchar, TRO.ROUNDNO) + '-' + 
		    CONVERT(varchar, TKU.SUBNO) + '-' + 
			CONVERT(varchar, TRO.KUMISUU) + '-' + 
			CONVERT(varchar, KG.SYUMOKUKUBUN) + '-' + 
			CONVERT(varchar, KG.SUBSYUMOKUKUBUN) + '-' + 
			CONVERT(varchar, KG.GATTAIFLG)
			 + '-' + CONVERT(varchar,KG.SUBKIROKUKUBUN)
		   ) AS KEKKAFLG,
		   SUBSTRING(TKU.KAISI,1,2) + ':' + SUBSTRING(TKU.KAISI,3,4) AS KAISI,
		   SUBSTRING(TKU.SYOSYU,1,2) + ':' + SUBSTRING(TKU.SYOSYU,3,4) AS SYOSYU,
		   CONVERT(varchar, dbo.Basyo(TKU.BASYO)) AS BASYO,
		   KG.KYOGIMEI ,
		   TRO.ROUNDMEI,
		   TKU.KUMINO,
		   CONVERT(varchar, dbo.Tyakudori(TRO.TYAKUDORI, TRO.PLUS)) AS TYAKUDORI,
		   dbo.RollCall(TKU.HOST, TKU.ROLLCALLFLG, TKU.KINOUFLG)AS ROLLCALLFLG,
		   TRO.TUKAKIROKU,
		   dbo.Kumibun(TRO.HOST, TRO.KUMIWAKEFLG) AS KUMIWAKEFLG,
		   dbo.Result(KG.SYUMOKUKUBUN,TKU.KYOGIFLG, TKU.KINOUFLG, TKU.HOST) AS RESULT,
		   dbo.KyogiFlg(TRO.HOST,TKU.KYOGIFLG) AS KYOGIFLG
		   -- 20190212 뢇맫SEQ.999 걁빥귂룈뢇맫걂
		   --,ROW_NUMBER() OVER (PARTITION BY KG.TAIKAINO ORDER BY TKU.KAISI, KG.SEIBETUNO, KG.SYUBETU_PRINTNO, KG.SYUMOKUNO, KG.KONSEIID) AS NUMBER
		  -- 20190612 뢇맫SEQ.999 걁빥귂룈뢇맫걂
		   --, ROW_NUMBER() OVER (PARTITION BY KG.TAIKAINO ORDER BY TKU.KAISI, KG.SEIBETUNO, KG.SYUBETU_PRINTNO, KG.SYUMOKU_PRINTNO, KG.KONSEIID) AS NUMBER
		   , ROW_NUMBER() OVER (PARTITION BY KG.TAIKAINO ORDER BY TKU.KAISI, KG.SEIBETUNO, KG.SYUBETU_PRINTNO, KG.SYUMOKU_PRINTNO, KG.KONSEIID, TKU.KUMINO) AS NUMBER
	  FROM TBL_KUMI AS TKU, 
	       TBL_ROUND AS TRO,
	       --KG TBL
		   (SELECT TK.TAIKAINO, 
				   TK.KYOGINO, 
				   TK.KYOGIMEI + SUB.SUBMEI AS KYOGIMEI, 
				   TK.KYOGIKANA, 
				   TK.SEIBETUNO, 
				   TSM.SYUMOKUNO, 
				   TSM.SYUMOKUMEI, 
				   TSM.KIROKUKUBUN, 
				   TSM.SYUMOKUKUBUN,
				   SUB.SUBMEI, 
				   SUB.SUBNO, 
				   SUB.KIROKUKUBUN AS SUBKIROKUKUBUN, 
				   SUB.SYUMOKUKUBUN AS SUBSYUMOKUKUBUN, 
				   TS.PRINTNO AS SYUBETU_PRINTNO, 
				   -- 20190212 뢇맫SEQ.999 걁빥귂룈뢇맫걂
				   --SUB.PRINTNO AS SYUMOKU_PRINTNO,
				   TSM.PRINTNO AS SYUMOKU_PRINTNO, 
				   SUB.KONSEIID, 
				   TK.GATTAIFLG

			 FROM TBL_KYOGI AS TK, 
				  TBL_SYUBETU AS TS, 
				  TBL_SYUMOKU_MASTER AS TSM, 
				  (SELECT K.TAIKAINO, 
						  K.KYOGINO, 
						  K.SYUMOKUNO, 
						  K.KYOGIMEI, 
						  K.KYOGIKANA, 
						  0 AS SUBNO, 
						  '' AS SUBMEI,
						  K.SEIBETUNO, 
						  SM.KIROKUKUBUN, 
						  SM.SYUMOKUKUBUN, 
						  SM.PRINTNO, 
						  0 AS KONSEIID
						 ,0 AS KONSEINO
					FROM TBL_KYOGI AS K, 
						 TBL_SYUMOKU_MASTER AS SM
					  WHERE K.TAIKAINO = @TAIKAINO
						AND K.SYUMOKUNO = SM.SYUMOKUNO
						AND (SM.KONSEISYUMOKUSUU = 0 OR SM.KONSEISYUMOKUSUU IS NULL)
					UNION
					  SELECT K.TAIKAINO, 
							 K.KYOGINO, 
							 K.SYUMOKUNO, 
							 K.KYOGIMEI, 
							 K.KYOGIKANA, 
							 SM.SYUMOKUNO AS SUBNO, 
							 SM.SYUMOKUMEI AS SUBMEI,
							 K.SEIBETUNO, 
							 SM.KIROKUKUBUN, 
							 SM.SYUMOKUKUBUN, 
							 SM.PRINTNO, 
							 KM.ID AS KONSEIID
							 ,KM.KONSEINO AS KONSEINO
						FROM TBL_KYOGI AS K, 
							 TBL_SYUMOKU_MASTER AS SM, 
							 TBL_KONSEI_MASTER AS KM
						WHERE K.TAIKAINO = @TAIKAINO
						  AND K.SYUMOKUNO = KM.SYUMOKUNO
						  AND KM.KONSEINO = SM.SYUMOKUNO
				  ) AS SUB
			 WHERE TK.TAIKAINO = @TAIKAINO
			   AND TK.TAIKAINO = SUB.TAIKAINO
			   AND TK.SYUMOKUNO = SUB.SYUMOKUNO
			   AND TK.SYUMOKUNO = TSM.SYUMOKUNO
			   AND TK.KYOGINO = SUB.KYOGINO
			   AND TS.SYUBETUNO = TK.SYUBETUNO
			   AND (TK.GATTAIFLG = 0 OR TK.GATTAIFLG = 2)
		   ) AS KG
		   WHERE TKU.TAIKAINO = @TAIKAINO
			 AND TKU.TAIKAINO = KG.TAIKAINO
			 AND TKU.KYOGINO = KG.KYOGINO
			 AND TKU.SUBNO = KG.SUBNO
			 AND TKU.TAIKAINO = TRO.TAIKAINO
			 AND TKU.KYOGINO = TRO.KYOGINO
			 AND TKU.SUBNO = TRO.SUBNO
			 AND TKU.ROUNDNO = TRO.ROUNDNO
			 AND TRO.HIDUKE LIKE @HIDUKE

AND KG.SYUMOKUKUBUN in (7,8)
AND (SUBSYUMOKUKUBUN LIKE CASE WHEN KG.SYUMOKUKUBUN = 8 THEN 7 ELSE KG.SYUMOKUKUBUN END)
--
			 AND KG.SEIBETUNO LIKE @SEIBETUNO
		-- 20190212 뢇맫SEQ.999 걁빥귂룈뢇맫걂
		-- ORDER BY TKU.KAISI, KG.SEIBETUNO, KG.SYUBETU_PRINTNO, KG.SYUMOKUNO, KG.KONSEIID
		-- 20190612 뢇맫SEQ.999 걁빥귂룈뢇맫걂
		-- ORDER BY TKU.KAISI, KG.SEIBETUNO, KG.SYUBETU_PRINTNO, KG.SYUMOKU_PRINTNO, KG.KONSEIID
		ORDER BY TKU.KAISI, KG.SEIBETUNO, KG.SYUBETU_PRINTNO, KG.SYUMOKU_PRINTNO, KG.KONSEIID, TKU.KUMINO
END
else IF @SYUMOKU='4'
BEGIN
	SELECT (CONVERT(varchar, KG.TAIKAINO) + '-' + 
	        CONVERT(varchar, TKU.KYOGINO) + '-' + 
		    CONVERT(varchar, TRO.ROUNDNO) + '-' + 
		    CONVERT(varchar, TKU.SUBNO) + '-' + 
			CONVERT(varchar, TRO.KUMISUU) + '-' + 
			CONVERT(varchar, KG.SYUMOKUKUBUN) + '-' + 
			CONVERT(varchar, KG.SUBSYUMOKUKUBUN) + '-' + 
			CONVERT(varchar, KG.GATTAIFLG)
			 + '-' + CONVERT(varchar,KG.SUBKIROKUKUBUN) 
		   ) AS KEKKAFLG,
		   SUBSTRING(TKU.KAISI,1,2) + ':' + SUBSTRING(TKU.KAISI,3,4) AS KAISI,
		   SUBSTRING(TKU.SYOSYU,1,2) + ':' + SUBSTRING(TKU.SYOSYU,3,4) AS SYOSYU,
		   CONVERT(varchar, dbo.Basyo(TKU.BASYO)) AS BASYO,
		   KG.KYOGIMEI ,
		   TRO.ROUNDMEI,
		   TKU.KUMINO,
		   CONVERT(varchar, dbo.Tyakudori(TRO.TYAKUDORI, TRO.PLUS)) AS TYAKUDORI,
		   dbo.RollCall(TKU.HOST, TKU.ROLLCALLFLG, TKU.KINOUFLG)AS ROLLCALLFLG,
		   TRO.TUKAKIROKU,
		   dbo.Kumibun(TRO.HOST, TRO.KUMIWAKEFLG) AS KUMIWAKEFLG,
		   dbo.Result(KG.SYUMOKUKUBUN,TKU.KYOGIFLG, TKU.KINOUFLG, TKU.HOST) AS RESULT,
		   dbo.KyogiFlg(TRO.HOST,TKU.KYOGIFLG) AS KYOGIFLG
		   -- 20190212 뢇맫SEQ.999 걁빥귂룈뢇맫걂
		   -- ,ROW_NUMBER() OVER (PARTITION BY KG.TAIKAINO ORDER BY TKU.KAISI, KG.SEIBETUNO, KG.SYUBETU_PRINTNO, KG.SYUMOKUNO, KG.KONSEIID) AS NUMBER
		   -- 20190612 뢇맫SEQ.999 걁빥귂룈뢇맫걂
		   -- , ROW_NUMBER() OVER (PARTITION BY KG.TAIKAINO ORDER BY TKU.KAISI, KG.SEIBETUNO, KG.SYUBETU_PRINTNO, KG.SYUMOKU_PRINTNO, KG.KONSEIID) AS NUMBER
		   , ROW_NUMBER() OVER (PARTITION BY KG.TAIKAINO ORDER BY TKU.KAISI, KG.SEIBETUNO, KG.SYUBETU_PRINTNO, KG.SYUMOKU_PRINTNO, KG.KONSEIID, TKU.KUMINO) AS NUMBER
	  FROM TBL_KUMI AS TKU, 
	       TBL_ROUND AS TRO,
	       --KG TBL
		   (SELECT TK.TAIKAINO, 
				   TK.KYOGINO, 
				   TK.KYOGIMEI + SUB.SUBMEI AS KYOGIMEI, 
				   TK.KYOGIKANA, 
				   TK.SEIBETUNO, 
				   TSM.SYUMOKUNO, 
				   TSM.SYUMOKUMEI, 
				   TSM.KIROKUKUBUN, 
				   TSM.SYUMOKUKUBUN,
				   SUB.SUBMEI, 
				   SUB.SUBNO, 
				   SUB.KIROKUKUBUN AS SUBKIROKUKUBUN, 
				   SUB.SYUMOKUKUBUN AS SUBSYUMOKUKUBUN, 
				   TS.PRINTNO AS SYUBETU_PRINTNO, 
				   -- 20190212 뢇맫SEQ.999 걁빥귂룈뢇맫걂
				   -- SUB.PRINTNO AS SYUMOKU_PRINTNO, 
				   TSM.PRINTNO AS SYUMOKU_PRINTNO, 
				   SUB.KONSEIID, 
				   TK.GATTAIFLG
				   
			 FROM TBL_KYOGI AS TK, 
				  TBL_SYUBETU AS TS, 
				  TBL_SYUMOKU_MASTER AS TSM, 
				  (SELECT K.TAIKAINO, 
						  K.KYOGINO, 
						  K.SYUMOKUNO, 
						  K.KYOGIMEI, 
						  K.KYOGIKANA, 
						  0 AS SUBNO, 
						  '' AS SUBMEI,
						  K.SEIBETUNO, 
						  SM.KIROKUKUBUN, 
						  SM.SYUMOKUKUBUN, 
						  SM.PRINTNO, 
						  0 AS KONSEIID
						 ,0 AS KONSEINO
					FROM TBL_KYOGI AS K, 
						 TBL_SYUMOKU_MASTER AS SM
					  WHERE K.TAIKAINO = @TAIKAINO
						AND K.SYUMOKUNO = SM.SYUMOKUNO
						AND (SM.KONSEISYUMOKUSUU = 0 OR SM.KONSEISYUMOKUSUU IS NULL)
					UNION
					  SELECT K.TAIKAINO, 
							 K.KYOGINO, 
							 K.SYUMOKUNO, 
							 K.KYOGIMEI, 
							 K.KYOGIKANA, 
							 SM.SYUMOKUNO AS SUBNO, 
							 SM.SYUMOKUMEI AS SUBMEI,
							 K.SEIBETUNO, 
							 SM.KIROKUKUBUN, 
							 SM.SYUMOKUKUBUN, 
							 SM.PRINTNO, 
							 KM.ID AS KONSEIID
							 ,KM.KONSEINO AS KONSEINO
						FROM TBL_KYOGI AS K, 
							 TBL_SYUMOKU_MASTER AS SM, 
							 TBL_KONSEI_MASTER AS KM
						WHERE K.TAIKAINO = @TAIKAINO
						  AND K.SYUMOKUNO = KM.SYUMOKUNO
						  AND KM.KONSEINO = SM.SYUMOKUNO
				  ) AS SUB
			 WHERE TK.TAIKAINO = @TAIKAINO
			   AND TK.TAIKAINO = SUB.TAIKAINO
			   AND TK.SYUMOKUNO = SUB.SYUMOKUNO
			   AND TK.SYUMOKUNO = TSM.SYUMOKUNO
			   AND TK.KYOGINO = SUB.KYOGINO
			   AND TS.SYUBETUNO = TK.SYUBETUNO
			   AND (TK.GATTAIFLG = 0 OR TK.GATTAIFLG = 2)
		   ) AS KG
		   WHERE TKU.TAIKAINO = @TAIKAINO
			 AND TKU.TAIKAINO = KG.TAIKAINO
			 AND TKU.KYOGINO = KG.KYOGINO
			 AND TKU.SUBNO = KG.SUBNO
			 AND TKU.TAIKAINO = TRO.TAIKAINO
			 AND TKU.KYOGINO = TRO.KYOGINO
			 AND TKU.SUBNO = TRO.SUBNO
			 AND TKU.ROUNDNO = TRO.ROUNDNO
			 AND TRO.HIDUKE LIKE @HIDUKE

AND KG.SYUMOKUKUBUN in (9,10,11)
--
			 AND KG.SEIBETUNO LIKE @SEIBETUNO
	-- 20190212 뢇맫SEQ.999 걁빥귂룈뢇맫걂
	-- ORDER BY TKU.KAISI, KG.SEIBETUNO, KG.SYUBETU_PRINTNO, KG.SYUMOKUNO, KG.KONSEIID
	-- 20190612 뢇맫SEQ.999 걁빥귂룈뢇맫걂
	--ORDER BY TKU.KAISI, KG.SEIBETUNO, KG.SYUBETU_PRINTNO, KG.SYUMOKU_PRINTNO, KG.KONSEIID
	ORDER BY TKU.KAISI, KG.SEIBETUNO, KG.SYUBETU_PRINTNO, KG.SYUMOKU_PRINTNO, KG.KONSEIID, TKU.KUMINO
END
else IF @SYUMOKU='5'
BEGIN
	SELECT (CONVERT(varchar, KG.TAIKAINO) + '-' + 
	        CONVERT(varchar, TKU.KYOGINO) + '-' + 
		    CONVERT(varchar, TRO.ROUNDNO) + '-' + 
		    CONVERT(varchar, TKU.SUBNO) + '-' + 
			CONVERT(varchar, TRO.KUMISUU) + '-' + 
			CONVERT(varchar, KG.SYUMOKUKUBUN) + '-' + 
			CONVERT(varchar, KG.SUBSYUMOKUKUBUN) + '-' + 
			CONVERT(varchar, KG.GATTAIFLG)
			 + '-' + CONVERT(varchar,KG.SUBKIROKUKUBUN)
		   ) AS KEKKAFLG,
		   SUBSTRING(TKU.KAISI,1,2) + ':' + SUBSTRING(TKU.KAISI,3,4) AS KAISI,
		   SUBSTRING(TKU.SYOSYU,1,2) + ':' + SUBSTRING(TKU.SYOSYU,3,4) AS SYOSYU,
		   CONVERT(varchar, dbo.Basyo(TKU.BASYO)) AS BASYO,
		   KG.KYOGIMEI ,
		   TRO.ROUNDMEI,
		   TKU.KUMINO,
		   CONVERT(varchar, dbo.Tyakudori(TRO.TYAKUDORI, TRO.PLUS)) AS TYAKUDORI,
		   dbo.RollCall(TKU.HOST, TKU.ROLLCALLFLG, TKU.KINOUFLG)AS ROLLCALLFLG,
		   TRO.TUKAKIROKU,
		   dbo.Kumibun(TRO.HOST, TRO.KUMIWAKEFLG) AS KUMIWAKEFLG,
		   dbo.Result(KG.SYUMOKUKUBUN,TKU.KYOGIFLG, TKU.KINOUFLG, TKU.HOST) AS RESULT,
		   dbo.KyogiFlg(TRO.HOST,TKU.KYOGIFLG) AS KYOGIFLG
		   -- 20190212 뢇맫SEQ.999 걁빥귂룈뢇맫걂
		   --,ROW_NUMBER() OVER (PARTITION BY KG.TAIKAINO ORDER BY TKU.KAISI, KG.SEIBETUNO, KG.SYUBETU_PRINTNO, KG.SYUMOKUNO, KG.KONSEIID) AS NUMBER
	      -- 20190612 뢇맫SEQ.999 걁빥귂룈뢇맫걂
		   --, ROW_NUMBER() OVER (PARTITION BY KG.TAIKAINO ORDER BY TKU.KAISI, KG.SEIBETUNO, KG.SYUBETU_PRINTNO, KG.SYUMOKU_PRINTNO, KG.KONSEIID) AS NUMBER
		   , ROW_NUMBER() OVER (PARTITION BY KG.TAIKAINO ORDER BY TKU.KAISI, KG.SEIBETUNO, KG.SYUBETU_PRINTNO, KG.SYUMOKU_PRINTNO, KG.KONSEIID, TKU.KUMINO) AS NUMBER
	  FROM TBL_KUMI AS TKU, 
	       TBL_ROUND AS TRO,
	       --KG TBL
		   (SELECT TK.TAIKAINO, 
				   TK.KYOGINO, 
				   TK.KYOGIMEI + SUB.SUBMEI AS KYOGIMEI, 
				   TK.KYOGIKANA, 
				   TK.SEIBETUNO, 
				   TSM.SYUMOKUNO, 
				   TSM.SYUMOKUMEI, 
				   TSM.KIROKUKUBUN, 
				   TSM.SYUMOKUKUBUN,
				   SUB.SUBMEI, 
				   SUB.SUBNO, 
				   SUB.KIROKUKUBUN AS SUBKIROKUKUBUN, 
				   SUB.SYUMOKUKUBUN AS SUBSYUMOKUKUBUN, 
				   TS.PRINTNO AS SYUBETU_PRINTNO, 
				   -- 20190212 뢇맫SEQ.999 걁빥귂룈뢇맫걂
				   -- SUB.PRINTNO AS SYUMOKU_PRINTNO, 
				   TSM.PRINTNO AS SYUMOKU_PRINTNO,
				   SUB.KONSEIID, 
				   TK.GATTAIFLG

			 FROM TBL_KYOGI AS TK, 
				  TBL_SYUBETU AS TS, 
				  TBL_SYUMOKU_MASTER AS TSM, 
				  (SELECT K.TAIKAINO, 
						  K.KYOGINO, 
						  K.SYUMOKUNO, 
						  K.KYOGIMEI, 
						  K.KYOGIKANA, 
						  0 AS SUBNO, 
						  '' AS SUBMEI,
						  K.SEIBETUNO, 
						  SM.KIROKUKUBUN, 
						  SM.SYUMOKUKUBUN, 
						  SM.PRINTNO, 
						  0 AS KONSEIID
						 ,0 AS KONSEINO
					FROM TBL_KYOGI AS K, 
						 TBL_SYUMOKU_MASTER AS SM
					  WHERE K.TAIKAINO = @TAIKAINO
						AND K.SYUMOKUNO = SM.SYUMOKUNO
						AND (SM.KONSEISYUMOKUSUU = 0 OR SM.KONSEISYUMOKUSUU IS NULL)
					UNION
					  SELECT K.TAIKAINO, 
							 K.KYOGINO, 
							 K.SYUMOKUNO, 
							 K.KYOGIMEI, 
							 K.KYOGIKANA, 
							 SM.SYUMOKUNO AS SUBNO, 
							 SM.SYUMOKUMEI AS SUBMEI,
							 K.SEIBETUNO, 
							 SM.KIROKUKUBUN, 
							 SM.SYUMOKUKUBUN, 
							 SM.PRINTNO, 
							 KM.ID AS KONSEIID
							 ,KM.KONSEINO AS KONSEINO
						FROM TBL_KYOGI AS K, 
							 TBL_SYUMOKU_MASTER AS SM, 
							 TBL_KONSEI_MASTER AS KM
						WHERE K.TAIKAINO = @TAIKAINO
						  AND K.SYUMOKUNO = KM.SYUMOKUNO
						  AND KM.KONSEINO = SM.SYUMOKUNO
				  ) AS SUB
			 WHERE TK.TAIKAINO = @TAIKAINO
			   AND TK.TAIKAINO = SUB.TAIKAINO
			   AND TK.SYUMOKUNO = SUB.SYUMOKUNO
			   AND TK.SYUMOKUNO = TSM.SYUMOKUNO
			   AND TK.KYOGINO = SUB.KYOGINO
			   AND TS.SYUBETUNO = TK.SYUBETUNO
			   AND (TK.GATTAIFLG = 0 OR TK.GATTAIFLG = 2)
		   ) AS KG
		   WHERE TKU.TAIKAINO = @TAIKAINO
			 AND TKU.TAIKAINO = KG.TAIKAINO
			 AND TKU.KYOGINO = KG.KYOGINO
			 AND TKU.SUBNO = KG.SUBNO
			 AND TKU.TAIKAINO = TRO.TAIKAINO
			 AND TKU.KYOGINO = TRO.KYOGINO
			 AND TKU.SUBNO = TRO.SUBNO
			 AND TKU.ROUNDNO = TRO.ROUNDNO
			 AND TRO.HIDUKE LIKE @HIDUKE

AND KG.SYUMOKUKUBUN in (8)
--
			 AND KG.SEIBETUNO LIKE @SEIBETUNO
	-- 20190212 뢇맫SEQ.999 걁빥귂룈뢇맫걂
	-- ORDER BY TKU.KAISI, KG.SEIBETUNO, KG.SYUBETU_PRINTNO, KG.SYUMOKUNO, KG.KONSEIID	
	-- 20190612 뢇맫SEQ.999 걁빥귂룈뢇맫걂
	-- ORDER BY TKU.KAISI, KG.SEIBETUNO, KG.SYUBETU_PRINTNO, KG.SYUMOKU_PRINTNO, KG.KONSEIID
	ORDER BY TKU.KAISI, KG.SEIBETUNO, KG.SYUBETU_PRINTNO, KG.SYUMOKU_PRINTNO, KG.KONSEIID, TKU.KUMINO
END

ELSE
BEGIN
	SELECT (CONVERT(varchar, KG.TAIKAINO) + '-' + 
	        CONVERT(varchar, TKU.KYOGINO) + '-' + 
		    CONVERT(varchar, TRO.ROUNDNO) + '-' + 
		    CONVERT(varchar, TKU.SUBNO) + '-' + 
			CONVERT(varchar, TRO.KUMISUU) + '-' + 
			CONVERT(varchar, KG.SYUMOKUKUBUN) + '-' + 
			CONVERT(varchar, KG.SUBSYUMOKUKUBUN) + '-' + 
			CONVERT(varchar, KG.GATTAIFLG)

			 + '-' + CONVERT(varchar,KG.SUBKIROKUKUBUN)
		   ) AS KEKKAFLG,
		   SUBSTRING(TKU.KAISI,1,2) + ':' + SUBSTRING(TKU.KAISI,3,4) AS KAISI,
		   SUBSTRING(TKU.SYOSYU,1,2) + ':' + SUBSTRING(TKU.SYOSYU,3,4) AS SYOSYU,
		   CONVERT(varchar, dbo.Basyo(TKU.BASYO)) AS BASYO,
		   KG.KYOGIMEI ,
		   TRO.ROUNDMEI,
		   TKU.KUMINO,
		   CONVERT(varchar, dbo.Tyakudori(TRO.TYAKUDORI, TRO.PLUS)) AS TYAKUDORI,
		   dbo.RollCall(TKU.HOST, TKU.ROLLCALLFLG, TKU.KINOUFLG)AS ROLLCALLFLG,
		   TRO.TUKAKIROKU,
		   dbo.Kumibun(TRO.HOST, TRO.KUMIWAKEFLG) AS KUMIWAKEFLG,
		   dbo.Result(KG.SYUMOKUKUBUN,TKU.KYOGIFLG, TKU.KINOUFLG, TKU.HOST) AS RESULT,
		   dbo.KyogiFlg(TRO.HOST,TKU.KYOGIFLG) AS KYOGIFLG,
		   -- 20190212 뢇맫SEQ.999 걁빥귂룈뢇맫걂
		   -- ROW_NUMBER() OVER (PARTITION BY KG.TAIKAINO ORDER BY TKU.KAISI, KG.SEIBETUNO, KG.SYUBETU_PRINTNO, KG.SYUMOKUNO, KG.KONSEIID) AS NUMBER
		   -- 20190612 뢇맫SEQ.999 걁빥귂룈뢇맫걂
		   -- ROW_NUMBER() OVER (PARTITION BY KG.TAIKAINO ORDER BY TKU.KAISI, KG.SEIBETUNO, KG.SYUBETU_PRINTNO, KG.SYUMOKU_PRINTNO, KG.KONSEIID) AS NUMBER
		   ROW_NUMBER() OVER (PARTITION BY KG.TAIKAINO ORDER BY TKU.KAISI, KG.SEIBETUNO, KG.SYUBETU_PRINTNO, KG.SYUMOKU_PRINTNO, KG.KONSEIID, TKU.KUMINO) AS NUMBER
	  FROM TBL_KUMI AS TKU, 
	       TBL_ROUND AS TRO,
	       --KG TBL
		   (SELECT TK.TAIKAINO, 
				   TK.KYOGINO, 
				   TK.KYOGIMEI + SUB.SUBMEI AS KYOGIMEI, 
				   TK.KYOGIKANA, 
				   TK.SEIBETUNO, 
				   TSM.SYUMOKUNO, 
				   TSM.SYUMOKUMEI, 
				   TSM.KIROKUKUBUN, 
				   TSM.SYUMOKUKUBUN,
				   SUB.SUBMEI, 
				   SUB.SUBNO, 
				   SUB.KIROKUKUBUN AS SUBKIROKUKUBUN, 
				   SUB.SYUMOKUKUBUN AS SUBSYUMOKUKUBUN, 
				   TS.PRINTNO AS SYUBETU_PRINTNO, 
				   -- 20190212 뢇맫SEQ.999 걁빥귂룈뢇맫걂
				   -- SUB.PRINTNO AS SYUMOKU_PRINTNO, 
				   TSM.PRINTNO AS SYUMOKU_PRINTNO,
				   SUB.KONSEIID, 
				   TK.GATTAIFLG
				   
			 FROM TBL_KYOGI AS TK, 
				  TBL_SYUBETU AS TS, 
				  TBL_SYUMOKU_MASTER AS TSM, 
				  (SELECT K.TAIKAINO, 
						  K.KYOGINO, 
						  K.SYUMOKUNO, 
						  K.KYOGIMEI, 
						  K.KYOGIKANA, 
						  0 AS SUBNO, 
						  '' AS SUBMEI,
						  K.SEIBETUNO, 
						  SM.KIROKUKUBUN, 
						  SM.SYUMOKUKUBUN, 
						  SM.PRINTNO, 
						  0 AS KONSEIID
						 ,0 AS KONSEINO
					FROM TBL_KYOGI AS K, 
						 TBL_SYUMOKU_MASTER AS SM
					  WHERE K.TAIKAINO = @TAIKAINO
						AND K.SYUMOKUNO = SM.SYUMOKUNO
						AND (SM.KONSEISYUMOKUSUU = 0 OR SM.KONSEISYUMOKUSUU IS NULL)
					UNION
					  SELECT K.TAIKAINO, 
							 K.KYOGINO, 
							 K.SYUMOKUNO, 
							 K.KYOGIMEI, 
							 K.KYOGIKANA, 
							 SM.SYUMOKUNO AS SUBNO, 
							 SM.SYUMOKUMEI AS SUBMEI,
							 K.SEIBETUNO, 
							 SM.KIROKUKUBUN, 
							 SM.SYUMOKUKUBUN, 
							 SM.PRINTNO, 
							 KM.ID AS KONSEIID
							 ,KM.KONSEINO AS KONSEINO
						FROM TBL_KYOGI AS K, 
							 TBL_SYUMOKU_MASTER AS SM, 
							 TBL_KONSEI_MASTER AS KM
						WHERE K.TAIKAINO = @TAIKAINO
						  AND K.SYUMOKUNO = KM.SYUMOKUNO
						  AND KM.KONSEINO = SM.SYUMOKUNO
				  ) AS SUB
			 WHERE TK.TAIKAINO = @TAIKAINO
			   AND TK.TAIKAINO = SUB.TAIKAINO
			   AND TK.SYUMOKUNO = SUB.SYUMOKUNO
			   AND TK.SYUMOKUNO = TSM.SYUMOKUNO
			   AND TK.KYOGINO = SUB.KYOGINO
			   AND TS.SYUBETUNO = TK.SYUBETUNO
			   AND (TK.GATTAIFLG = 0 OR TK.GATTAIFLG = 2)
		   ) AS KG
		   WHERE TKU.TAIKAINO = @TAIKAINO
			 AND TKU.TAIKAINO = KG.TAIKAINO
			 AND TKU.KYOGINO = KG.KYOGINO
			 AND TKU.SUBNO = KG.SUBNO
			 AND TKU.TAIKAINO = TRO.TAIKAINO
			 AND TKU.KYOGINO = TRO.KYOGINO
			 AND TKU.SUBNO = TRO.SUBNO
			 AND TKU.ROUNDNO = TRO.ROUNDNO
			 AND TRO.HIDUKE LIKE @HIDUKE

AND KG.SYUMOKUKUBUN LIKE '%'
--
			 AND KG.SEIBETUNO LIKE @SEIBETUNO
	-- 20190212 뢇맫SEQ.999 걁빥귂룈뢇맫걂
	-- ORDER BY TKU.KAISI, KG.SEIBETUNO, KG.SYUBETU_PRINTNO, KG.SYUMOKUNO, KG.KONSEIID
	-- 20190612 뢇맫SEQ.999 걁빥귂룈뢇맫걂
	-- ORDER BY TKU.KAISI, KG.SEIBETUNO, KG.SYUBETU_PRINTNO, KG.SYUMOKU_PRINTNO, KG.KONSEIID
	ORDER BY TKU.KAISI, KG.SEIBETUNO, KG.SYUBETU_PRINTNO, KG.SYUMOKU_PRINTNO, KG.KONSEIID, TKU.KUMINO
END